<?php

class Task {

    static $phpfile;
    static $logfile;
    static $init = false;

    public static function init($phpfile, $flag = 1) {
        self::$phpfile = $phpfile . $flag;
        self::$logfile = APP_DATA_PATH . 'tasklog/' . basename($phpfile) . '_' . $flag . '.txt';
        fCheckDir(self::$logfile);
        self::$init = true;
    }

    public static function autoReTry() {
        
    }

    public static function setCurrent($data) {
        if (!self::$init) {
            log::error('Task NOT init,Please Task::init(__FILE__);');
        }
        $edata = @json_decode(@file_get_contents(self::$logfile), true);
        $string = json_encode(array_merge((array) $edata, $data));
        file_put_contents(self::$logfile, $string);
    }

    public static function getPrevious() {
        if (!self::$init) {
            log::error('Task NOT init,Please Task::init(__FILE__);');
        }
        return @file_get_contents(self::$logfile);
    }

    public static function isGoOn($key, $value, $logmsg = '', $success = false) {
        if (!self::$init) {
            log::error('Task NOT init,Please Task::init(__FILE__);');
        }
        $prev = self::getPrevious();
        $arr = json_decode($prev, true);
        $prevValue = $arr[$key];
        if (!$success) {
            if ($value < $prevValue) {
                log::warn('JumpIng...' . $key . '|' . $value . '|' . $logmsg);
                return false;
            }
        } else {
            if ($value <= $prevValue) {
                log::warn('JumpIng...' . $key . '|' . $value . '|' . $logmsg);
                return false;
            }
        }
        return true;
    }

    public static function setKeyPage($key, $page = 1) {
        $data = array(
            'sys_index_key' => $key,
            'sys_index_page' => $page,
        );
        return self::setCurrent($data);
    }

    #是否跳过当前页面

    public static function isJumpKeyPage($key, $page = 1) {
        if (!self::$init) {
            log::error('Task NOT init,Please Task::init(__FILE__);');
        }
        $prev = self::getPrevious();
        $arr = json_decode($prev, true);
        if ($key < $arr['sys_index_key']) {
            log::w('Task Jump, Key:' . $key . ' < Sys Key:' . $arr['sys_index_key']);
            return true;
        }
        if ($key == $arr['sys_index_key']) {
            if ($page <= $arr['sys_index_page']) {
                log::w('Task Jump, Key:' . $key . ' == Sys Key:' . $arr['sys_index_key']);
                log::w('Task Jump, Page:' . $page . ' <= Sys Key:' . $arr['sys_index_page']);
                return true;
            }
        }
        return false;
    }

}

?>